﻿Module mdlGlobal
    ' Поулчить объект редактора по любому контролу
    Public Function GetEditor(ByVal shp As Object) As shpEditor
        Dim edi As shpEditor
        edi = TryCast(shp, shpEditor)
        While (shp IsNot Nothing) AndAlso (edi Is Nothing)
            shp = shp.Ancestor
            edi = TryCast(shp, shpEditor)
        End While
        Return edi
    End Function

    ' Расстояние между двумя точками
    Public Function DistanceBetweenTwoPoints(ByVal pnt1 As Point, ByVal pnt2 As Point) As Integer
        Dim dx As Integer = pnt1.X - pnt2.X, dy As Integer = pnt1.Y - pnt2.Y
        DistanceBetweenTwoPoints = Math.Sqrt(dx * dx + dy * dy)
    End Function

    ' Точка, находящаяся между двумя заданными точками
    Public Function AveragePoint(ByVal x1 As Integer, ByVal x2 As Integer) As Integer
        If x1 > x2 Then
            AveragePoint = x1 + (x2 - x1) / 2
        Else
            AveragePoint = x2 + (x1 - x2) / 2
        End If
    End Function

    ' Точка, находящаяся между двумя заданными точками, с заданным смещением от x1
    Public Function ShiftPoint(ByVal x1 As Integer, ByVal x2 As Integer, shift As Integer) As Integer
        If x1 <> x2 Then
            If x1 < x2 Then
                ShiftPoint = x1 + shift
            Else
                ShiftPoint = x1 - shift
            End If
        Else
            ShiftPoint = x1
        End If
    End Function
End Module
